package com.bwsoft.admin.action;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import net.sf.json.JSONObject;

import com.bwsoft.admin.domain.Advertisement;
import com.bwsoft.admin.domain.Banner;
import com.bwsoft.admin.domain.valueObject.AdvertisementVO;

public class AdAction extends BaseAction_ybd{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2094507569777554195L;

	//s_adName,  s_place,  s_status,  s_remark
	String s_adName;
	String s_place;
	String s_status;
	String s_remark;
	
	public String adIds;
	
	public String adIndex(){
		return "adIndex";
	}

	/**
	 * 查询获取列表
	 */
	public String getAdJson() {

		List<Advertisement> adSet=null;
		
			
		//通过字典编号查询字典详情的分页集合
		adSet = adService.findData(page, rows, s_adName,  s_place,  s_status,  s_remark);
		//通过编号查询字典集合的大小	
		Long count = adService.getTotalCount(s_adName,  s_place,  s_status,  s_remark);
		
		List<AdvertisementVO> adVo = adService.getAdVo(adSet);
		
		responseJson.put("total", count);
		responseJson.put("rows", adVo);

		return "mapJsonResult";
	}
	
	/**
	 * 保存或新增
	 * @return
	 */
	public String saveOrUpdateAd(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		if(ad.getAdid() == null || "".equals(ad.getAdid())){
			if(adImg == null || "".equals(adImg)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "请上传广告图！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "请上传广告图！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			if(adImg != null && !"".equals(adImg) && !newsService.isImage(adImgContentType)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "广告上传的文件不是图片类型！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "广告上传的文件不是图片类型！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			if(adImg != null && !"".equals(adImg) && !newsService.isFitSize(adImg)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "广告的图片不能超过8M！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "广告的图片不能超过8M！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		}
		msg = adService.saveOrUpdateAd(ad, adImg, adImgFileName);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取详情内容
	 * @return
	 */
	public String adModifyInfo(){
		ad = adService.adModifyInfo(ad.getAdid());
		
		List<Advertisement> adList = new ArrayList<Advertisement>();
		adList.add(ad);
		List<AdvertisementVO> adVo = adService.getAdVo(adList);
		
		responseJson.put("ad", adVo.get(0));
		return "mapJsonResult";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		
		msg = adService.remove(adIds);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 状态改为显示
	 * @return
	 */
	public String show(){
		
		
		msg = adService.show(adIds);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 状态改为隐藏
	 * @return
	 */
	public String hide(){
		
		msg = adService.hide(adIds);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
//	-----------------------------------------------------------------
	public String getS_adName() {
		return s_adName;
	}

	public void setS_adName(String s_adName) {
		this.s_adName = s_adName;
	}

	public String getS_place() {
		return s_place;
	}

	public void setS_place(String s_place) {
		this.s_place = s_place;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	public String getS_remark() {
		return s_remark;
	}

	public void setS_remark(String s_remark) {
		this.s_remark = s_remark;
	}

	public String getAdIds() {
		return adIds;
	}

	public void setAdIds(String adIds) {
		this.adIds = adIds;
	}
	
	
	
}
